local ret = {}

local skel = fk.CreateSkill {
  name = "rmt__fenjie",
  tags = {Skill.Limited},
}

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Start
    and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
  end,
  on_cost = function (self, event, target, player, data)
    local _, dat = player.room:askToUseActiveSkill(player, {
      skill_name = "rmt__fenjie_choose", prompt = "#rmt__fenjie-invoke", skip = true,
    })
    if dat then
      player.room:sortByAction(dat.targets)
      event:setCostData(self, {tos = dat.targets})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos---@type ServerPlayer[]
    room:setPlayerMark(player, "rmt__fenjie_tar", table.map(tos, Util.IdMapper))
    for _, to in ipairs(tos) do
      if not to.dead then
        to:drawCards(1, skel.name)
        if not to.dead and not to:isNude() and not player.dead and player ~= to then
          local cards = to:getCardIds("he")
          if #cards > 2 then
            cards = room:askToCards(to, {
              max_num = 2, min_num = 2, include_equip = true, skill_name = skel.name, cancelable = false,
              prompt = "#rmt__fenjie-give:"..player.id
            })
          end
          room:obtainCard(player, cards, false, fk.ReasonGive, to, skel.name)
        end
      end
    end
  end,
})

table.insert(ret, skel)

Fk:loadTranslationTable{
  ["rmt__fenjie"] = "分界",
  [":rmt__fenjie"] = "限定技，准备阶段，你可以令任意名与你横置状态相同且座次连续的角色各摸一张牌并交给你两张牌，本局这些角色因“屈起”重置后，你可以改为令其视为使用【桃】。  ",
  ["#rmt__fenjie-invoke"] = "分界：你可以令任意名与你横置状态相同且座次连续的角色各摸一张牌并交给你两张牌",
  ["rmt__fenjie_choose"] = "分界",
  ["#rmt__fenjie-give"] = "分界：请交给 %src 两张牌",
}

local skel2 = fk.CreateSkill {
  name = "rmt__fenjie_choose",
}

skel2:addEffect("active", {
  mute = true,
  card_num = 0,
  min_target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to, selected, selected_cards)
    if to and not to:isRemoved() and (to.chained == player.chained) then
      if #selected == 0 then
        return true
      else
        for _, p in ipairs(selected) do
          if to:getNextAlive() == p or p:getNextAlive() == to then
            return true
          end
        end
      end
    end
  end,
})

table.insert(ret, skel2)

return ret
